package com.galleryviewpager.a_paopao;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class PaoPaoView extends View {
    private static final String TAG = "PaoPaoView";

    private int mWidth;
    private int mHeight;
    private int mRectWidth;
    private int mRectHeigth;
    private float mRectPercent = 0.8f;
    private RectF rectF = new RectF();

    private Paint mPaint = new Paint();

    private Path path = new Path();

    public PaoPaoView(Context context) {
        this(context, null);
    }

    public PaoPaoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PaoPaoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        Log.e(TAG, "onMeasure: widthSize " + widthSize);
        switch (widthMode) {
            case MeasureSpec.AT_MOST: //尽可能大  wrap_content
                Log.e(TAG, "onMeasure: widthMode  尽可能大");
                mWidth = Math.min(500, widthSize);
                mRectWidth = ((int) (mWidth * mRectPercent));
                break;
            case MeasureSpec.EXACTLY:  //精确值  match_parent , width height
                Log.e(TAG, "onMeasure: widthMode  精确值");
                mWidth = widthSize;
                mRectWidth = ((int) (mWidth * mRectPercent));
                break;
            case MeasureSpec.UNSPECIFIED:  //没有约束 基本上遇不到
                Log.e(TAG, "onMeasure: widthMode  没有约束");
                break;
        }
        switch (heightMode) {
            case MeasureSpec.AT_MOST: //尽可能大
                mHeight = Math.min(500, heightSize);
                break;
            case MeasureSpec.EXACTLY:  //精确值
                mHeight = heightSize;
                mRectHeigth = (int) (mHeight * (mRectPercent + 0.1));
                break;
            case MeasureSpec.UNSPECIFIED:  //没有约束
                break;
        }
        setMeasuredDimension(mWidth, mHeight + 120);
        rectF.set(0, 0, mRectWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.e(TAG, "onDraw: " + mWidth);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(6f);

        canvas.drawRoundRect(rectF, 10, 10, mPaint);

        int i = mRectWidth / 2;
        path.moveTo(i - 60, mRectHeigth);
        path.lineTo(i, mRectHeigth + 40);
        path.lineTo(i + 60, mRectHeigth);
        path.close();
        canvas.drawPath(path, mPaint);

    }
}
